#include "HttpServer.hpp"
#include "file_utils.hpp"


void sendhtml(ConnectionPtr conn) {
    auto content = file_get_content("login.html");
    // std::cout << content << std::endl;
    HttpResponse resp;
    resp.addContent(content.c_str(), content.size());
    resp.addHeader("Content-Length", std::to_string(content.size()));
    resp.addHeader("Content-Type", "text/html; charset=utf-8");
    resp.writeBuffer(conn->getOutputBuffer());
    // conn->getOutputBuffer() -> printBuff();
    conn->writeSocket();
}

void sendico(ConnectionPtr conn) {
    auto content = file_get_content("favicon.ico");
    // std::cout << content << std::endl;
    HttpResponse resp;
    resp.addContent(content.c_str(), content.size());
    resp.addHeader("Content-Length", std::to_string(content.size()));
    resp.writeBuffer(conn->getOutputBuffer());
    // conn->getOutputBuffer() -> printBuff();
    conn->writeSocket();
}

void sendico2(ConnectionPtr conn) {
    auto content = file_get_content("bird.ico");
    // std::cout << content << std::endl;
    HttpResponse resp;
    resp.addContent(content.c_str(), content.size());
    resp.addHeader("Content-Length", std::to_string(content.size()));
    resp.writeBuffer(conn->getOutputBuffer());
    // conn->getOutputBuffer() -> printBuff();
    conn->writeSocket();
}

void login(ConnectionPtr conn) {
    std::string content = "{\"data\":\"这里是login\"}";
    std::cout << content << std::endl;
    HttpResponse resp;
    resp.addContent(content.c_str(), content.size());
    resp.addHeader("Content-Length", std::to_string(content.size()));
    resp.addHeader("Content-Type", "application/json; charset=utf-8");
    resp.writeBuffer(conn->getOutputBuffer());
    // conn->getOutputBuffer() -> printBuff();
    conn->writeSocket();
}

int main() {
    chdir("../resources");
    
    HttpServer server;
    server.setRoute("/", sendhtml);
    server.setRoute("/favicon.ico", sendico);
    server.setRoute("/bird.ico", sendico2);
    server.setRoute("/login", login);
    server.start(9999);
    return 0;
}